package com.cvit.controller;

import java.beans.IntrospectionException;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.cvit.model.Achievement;
import com.cvit.model.Msg;
import com.cvit.service.AchievementService;
import com.cvit.utils.Utils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class AchievementController {
	@Autowired
	AchievementService achievementService;
	
	/**
	 * 查询成绩表单（包含学生表信息）
	 * */
	@RequestMapping("/getAchievement")
	@ResponseBody
	public Msg getAchievements(@RequestParam(value = "pn", defaultValue = "1") Integer pn){
		PageHelper.startPage(pn, 5);
		List<Achievement> achievements = achievementService.getAchievement();
		PageInfo<Achievement> page = new PageInfo<Achievement>(achievements, 5);	
		return Msg.success().add("pageInfo", page);
}
	/**
	 * 查询成绩表单（包含学生表信息）
	 * */
	@RequestMapping("/getAchievement1")
	@ResponseBody
	public Msg getAchievements1(@RequestParam(value = "pn", defaultValue = "1") Integer pn){
		PageHelper.startPage(pn, 999999999);
		List<Achievement> achievements = achievementService.getAchievement();
		PageInfo<Achievement> page = new PageInfo<Achievement>(achievements, 999999999);	
		return Msg.success().add("pageInfo", page);
}
	
	/**
	 * 根据Id、学生序号、身份证号、考生号查询成绩表单
	 * */
	@RequestMapping("/getAchievementBy")
	@ResponseBody
	public Msg getAchievements(Achievement achievement){
		Achievement achievements = achievementService.getAchievementBy(achievement);
		return Msg.success().add("Achievements", achievements);
}
	/**
	 * 根据学生号查询成绩
	 * */
	@RequestMapping("/getAchievementByEamineeId")
	@ResponseBody
	public Msg getAchievementsByEamineeId(String EamineeId, Integer type){
		Achievement achievements = achievementService.getAchievementByEamineeId(EamineeId);
		Date day=new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyMMddHHmm");
		long ts1= Long.parseLong(df.format(day));
		long ts2 = 1803230830; 
		if(type==1){
		if(ts1<ts2){
			return Msg.fail().add("code", "成绩尚未公布");
		}}
		if(Utils.isNull(achievements)==true){
			return Msg.fail().add("code", "考生信息不存在！");
		}
		return Msg.success().add("Achievements", achievements);
	}
	/**
	 * 导入Excel
	 * */
	@RequestMapping("/AchievementImport")
	@ResponseBody
	public Msg impotr(HttpServletRequest request, Achievement achievement) throws Exception {
	     //获取上传的文件 
	     MultipartHttpServletRequest multipart = (MultipartHttpServletRequest) request;
	     MultipartFile file = multipart.getFile("upfile");
	     InputStream in = file.getInputStream();
	     //数据导入  
	     achievementService.importExcelInfo(in,file);
	     in.close();
	     return Msg.success();
	}
	
	/**
	 * 导出Excel
	 * @throws UnsupportedEncodingException 
	 * */
	@RequestMapping("/AchievementExport")  
	public @ResponseBody void export(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, IntrospectionException, IllegalAccessException, ParseException, InvocationTargetException, UnsupportedEncodingException {  
		String achievementData = "学生分数表";
	    if(achievementData!=""){
			response.reset(); //清除buffer缓存  
	        Map<String,Object> map=new HashMap<String,Object>();  
	        // 指定下载的文件名  
	        response.setHeader("Content-Disposition", "attachment;filename="  
			        + new String(achievementData.getBytes(),"iso-8859-1") + ".xlsx");
	        response.setHeader("Pragma", "no-cache");  
	        response.setHeader("Cache-Control", "no-cache");  
	        response.setDateHeader("Expires", 0);   
	        XSSFWorkbook workbook=null;  
	        //导出Excel对象  
	        workbook = achievementService.exportExcelInfo(achievementData);  
	        OutputStream output;  
	        try {
	            output = response.getOutputStream();  
	            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);  
	            bufferedOutPut.flush();  
	            workbook.write(bufferedOutPut);  
	            bufferedOutPut.close();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	    }  
	}
	
}
